<?php

/*	LibFox - Simple library for XML-based backends.
	Copyright (C) 2008 LinFox Serviços em Informática LTDA.

	This file is part of LibFox.

	LibFox is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	LibFox is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>. */

require_once 'dao.php';
require_once 'changes.php';

/**
 * Abstract class for controllers
 */
abstract class controller
{
	/**
	 * Controller's mode.
	 * 
	 * Only used when outputting xml. 
	 *
	 * @var string
	 */
	protected $mode = 'models';
	/**
	 * Controller's model.
	 * 
	 * Only used when outputting xml.
	 *
	 * @var string
	 */
	protected $model = 'model';
	/**
	 * Controller's DAO class.
	 * 
	 * Name of the Data Access Object class that the controller should use
	 * to fetch models and manipulate the database.
	 *
	 * @var string
	 */
	protected $dao = 'dao';

	public function __construct() { }

	/**
	 * Fetch a single model from the database and output it as xml.
	 *
	 * @return string
	 */
	public function show()
	{
		$dao = new $this->dao($this->db);
		$model = $dao->fetch(get($dao->key));
		return $model->xml_iterate($this->model);
	}

	/**
	 * Show all rows from this mode's table.
	 * 
	 * Result's can be restricted through the search request var.
	 *
	 * @return string
	 */
	public function show_all()
	{
		$dao = new $this->dao($this->db);
		$models = $dao->fetch_all(get('search'));
		$result = '<'.$this->mode.'>';
		foreach ($models as $model) {
			$result .= $model->xml_iterate($this->model);
		}
		$result .= '</'.$this->mode.'>';
		return $result;
	}

	/**
	 * Create a new model and get it's ID.
	 * 
	 * Return's the ID of the row that was created.
	 *
	 * @return string
	 */
	public function create()
	{
		$dao = new $this->dao($this->db);
		$model = $dao->create();
		changes::get_instance()->mark($this->mode);
		return $model->xml_iterate($this->model) . $this->show_all();
	}

	/**
	 * Insert a new model.
	 *
	 * @return unknown
	 */
	public function insert()
	{
		$dao = new $this->dao($this->db); 
		$model = $dao->fetch_model();
		$model->get_request();
		$dao->insert($model);
		changes::get_instance()->mark($this->mode);
		return $this->show_all(get('search'));
	}

	/**
	 * Update values of a model.
	 *
	 * Return's the updated model in XML
	 * 
	 * @return string
	 */
	public function update()
	{
		$dao = new $this->dao($this->db); 
		$model = $dao->fetch(get($dao->key));
		$model->get_request();
		$dao->update($model);
		changes::get_instance()->mark($this->mode);
		return $this->show_all(get('search'));
	}

	/**
	 * Delete a model.
	 *
	 * @return unknown
	 */
	public function delete()
	{
		$dao = new $this->dao($this->db);
		$dao->delete(get($dao->key));
		changes::get_instance()->mark($this->mode);
		return $this->show_all(get('search'));
	}

	/**
	 * Get a value from the $_POST or $_GET arrays.
	 *
	 * @param string $var
	 * @return string
	 */
	protected function get($var)
	{
		if		(isset($_POST[$var]))	return mysql_escape_string($_POST[$var]);
		elseif	(isset($_GET[$var]))	return mysql_escape_string($_GET[$var]);
		else							return false;
	}

	/**
	 * Get's ARP information from $arp_file and returns it in an array.
	 *
	 * @param string $arp_file
	 * @return array
	 */
	protected function arp($arp_file = '/proc/net/arp')
	{
		$arp = file($arp_file);
		$result = array();
		foreach($arp as $row_number => $row) {
			if ($row_number == '0') {
				$keys = split(" [ ]+", $row);
			} else {
				$values = split(" [ ]+", $row);
				foreach ($values as $key => $value) {
					$result[$row_number][str_underline(strtolower($keys[$key]))] = $value;
				}
			}
		}
		return $result;
	}
}


?>
